package com.snowRemovingUser.service.impl;

import com.snowRemovingUser.domain.MDevice;
import com.snowRemovingUser.domain.MDeviceAttribute;
import com.snowRemovingUser.dto.DeviceTreeSelect;
import com.snowRemovingUser.mapper.MDeviceAttributeMapper;
import com.snowRemovingUser.mapper.MDeviceMapper;
import com.snowRemovingUser.service.IMDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 设备实况Service业务层处理
 *
 * @author lishiguang
 * @date 2023-05-06
 */
@Service
public class MDeviceServiceImpl implements IMDeviceService
{
    @Autowired
    private MDeviceMapper mDeviceMapper;

    @Autowired
    private MDeviceAttributeMapper mDeviceAttributeMapper;


    /**
     * 查询设备实况
     *
     * @param serialNumber 设备实况主键
     * @return 设备实况
     */
    @Override
    public MDevice selectMDeviceBySerialNumber(String serialNumber)
    {
        return mDeviceMapper.selectMDeviceBySerialNumber(serialNumber);
    }

    /**
     * 查询设备实况列表
     *
     * @param mDevice 设备实况
     * @return 设备实况
     */
    @Override
    public List<MDevice> selectMDeviceList(MDevice mDevice)
    {
        return mDeviceMapper.selectMDeviceList(mDevice);
    }

    /**
     * 新增设备实况
     *
     * @param mDevice 设备实况
     * @return 结果
     */
    @Override
    public int insertMDevice(MDevice mDevice)
    {
        return mDeviceMapper.insertMDevice(mDevice);
    }

    /**
     * 修改设备实况
     *
     * @param mDevice 设备实况
     * @return 结果
     */
    @Override
    public int updateMDevice(MDevice mDevice)
    {
        return mDeviceMapper.updateMDevice(mDevice);
    }

    /**
     * 批量删除设备实况
     *
     * @param serialNumbers 需要删除的设备实况主键
     * @return 结果
     */
    @Override
    public int deleteMDeviceBySerialNumbers(String[] serialNumbers)
    {
        return mDeviceMapper.deleteMDeviceBySerialNumbers(serialNumbers);
    }

    /**
     * 删除设备实况信息
     *
     * @param serialNumber 设备实况主键
     * @return 结果
     */
    @Override
    public int deleteMDeviceBySerialNumber(String serialNumber)
    {
        return mDeviceMapper.deleteMDeviceBySerialNumber(serialNumber);
    }


    /**
     * 设备实况树
     * @param mDevice 设备实况
     * @return
     */
    @Override
    public List<DeviceTreeSelect> selectDeviceTreeList(MDevice mDevice) {


        List<DeviceTreeSelect> list = new ArrayList<>();

        List<MDevice> mDevices = mDeviceMapper.selectMDeviceList(mDevice);

        //将 mDevices 用map封装
        Map<String, List<MDevice>> deviceByRoadName = mDevices.stream().collect(Collectors.groupingBy(MDevice::getRoadName));
        Set<String> RoadNames = deviceByRoadName.keySet();

        for (String RoadName : RoadNames) {

            List<MDevice> mDevices1 = deviceByRoadName.get(RoadName);

            Map<String, List<MDevice>> deviceByTypeName = mDevices1.stream().collect(Collectors.groupingBy(MDevice::getDeviceTypeName));
            Set<String> TypeNames = deviceByTypeName.keySet();

            List<DeviceTreeSelect> list2 = new ArrayList<>();
            for (String TypeName : TypeNames) {

                List<MDevice> mDevices2 = deviceByTypeName.get(TypeName);

                List<DeviceTreeSelect> list3 = new ArrayList<>();
                for (MDevice device : mDevices2) {

                    DeviceTreeSelect deviceTreeSelect3 = new DeviceTreeSelect();
                    deviceTreeSelect3.setId(device.getSerialNumber());

                    String landmarkId = device.getLandmarkId();
                    String k = landmarkId.substring(landmarkId.indexOf("K"));
                    deviceTreeSelect3.setLabel(k);

                    String icon ="";
                    Integer overallStatus = device.getOverallStatus();

                    //overallStatus为0为正常状态
                    if (overallStatus == 0){
                        icon="";

                    //overallStatus为1为异常状态
                    }else if (overallStatus == 1){

                        icon="el-icon-warning";

                        //overallStatus为2为停用状态
                    }else if (overallStatus == 2){

                        icon="el-icon-link";
                    }

                    deviceTreeSelect3.setIcon(icon);
                    list3.add(deviceTreeSelect3);
                }

                DeviceTreeSelect deviceTreeSelect2 = new DeviceTreeSelect();
                deviceTreeSelect2.setId(mDevices2.get(0).getRoadId()+mDevices2.get(0).getDeviceType());
                deviceTreeSelect2.setLabel(TypeName);
                deviceTreeSelect2.setChildren(list3);
                list2.add(deviceTreeSelect2);
            }

            DeviceTreeSelect deviceTreeSelect1 = new DeviceTreeSelect();
            deviceTreeSelect1.setId(mDevices1.get(0).getRoadId());
            deviceTreeSelect1.setLabel(RoadName);
            deviceTreeSelect1.setChildren(list2);
            list.add(deviceTreeSelect1);
        }

        return list;
    }


    /**
     *  实况数据
     * @param
     * @return
     * @throws IOException
     */
    @Override
    public List<MDeviceAttribute> selectDeviceAttributeList(MDeviceAttribute mDeviceAttribute){


        //设备集合
        List<MDeviceAttribute> deviceAttributeList = mDeviceAttributeMapper.selectMDeviceAttributeList(mDeviceAttribute);
        return deviceAttributeList;

    }

}
